package com.iyao_app.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.iyao_app.R;
import com.iyao_app.entry.Pois;
import com.iyao_app.util.DistanceUtil;
import com.iyao_app.util.MarkUtil;
import com.iyao_app.util.ResourcesUtils;
import com.iyaowoqu.base_library.util.LogUtil;

import java.util.List;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/27.
 */

public class SearchAdapter_ extends BaseQuickAdapter<Pois, BaseViewHolder> {
    //地球平均半径
    private static final double EARTH_RADIUS = 6378137;
    LatLng myLatLng;

    public SearchAdapter_(@Nullable List<Pois> data, LatLng latLng) {
        super(R.layout.item_search, data);
        myLatLng = latLng;
    }


    @Override
    protected void convert(BaseViewHolder helper, Pois item) {
        SimpleDraweeView mSdvHead = helper.getView(R.id.sdv_head);
        TextView mTvLocation = helper.getView(R.id.tv_location);
        TextView mTvLocationDetail = helper.getView(R.id.tv_location_detail);
        TextView mTvDistance = helper.getView(R.id.tv_distance);
        helper.itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) ResourcesUtils.getDimension(R.dimen.x96)));
        mTvLocation.setText(item.getName());
        mTvLocationDetail.setText(item.getAddress());

        mTvDistance.setText(DistanceUtil.getDitance(getDistance(item)));
        mSdvHead.setImageResource(MarkUtil.getId(item.getTypecode()));

    }

    private double getDistance(Pois person) {
        String location = person.getLocation();
        if (TextUtils.isEmpty(location)) {
            return 0;
        }
        String[] split = location.split(",");
        double radLat1 = rad(myLatLng.latitude);
        double radLat2 = rad(Double.parseDouble(split[1]));
        double a = radLat1 - radLat2;
        double b = rad(myLatLng.longitude) - rad(Double.parseDouble(split[0]));
        double s = 2 * Math.asin(
                Math.sqrt(
                        Math.pow(Math.sin(a / 2), 2)
                                + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
                )
        );
        s = s * EARTH_RADIUS;
        LogUtil.e("=sMath===" + s);
        s = s / 1000.00d;
        LogUtil.e("=Math===" + s);
        return s;
    }


    //把经纬度转为度（°）
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

}
